package com.gitlab.lucky.common.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gitlab.lucky.common.mapper.AbstractMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.List;


public class ServiceImpl<M extends AbstractMapper<T>, T> implements AbstractService<T> {

    @Autowired
    protected M abstractMapper;

    /**
     * <p>
     * 判断数据库操作是否成功
     * </p>
     *
     * @param result 数据库操作返回影响条数
     * @return boolean
     */
    public static boolean retBool(Integer result) {
        return null != result && result >= 1;

    }
    public boolean insert(T record) {
        return retBool(this.abstractMapper.insert(record));
    }

    public boolean insertSelective(T record) {
        return retBool(this.abstractMapper.insertSelective(record));
    }

    public boolean insertList(List<T> recordList) {
        return retBool(this.abstractMapper.insertList(recordList));
    }

    public boolean insertUseGeneratedKeys(T record) {
        return retBool(this.abstractMapper.insertUseGeneratedKeys(record));
    }

    
    public boolean deleteByPrimaryKey(Serializable key) {
        return retBool(this.abstractMapper.deleteByPrimaryKey(key));
    }

    
    public boolean delete(T record) {
        return retBool(this.abstractMapper.delete(record));
    }

    
    public boolean deleteByExample(Example example) {
        return retBool(this.abstractMapper.deleteByExample(example));
    }

    
    public boolean updateByPrimaryKey(T record) {
        return retBool(this.abstractMapper.updateByPrimaryKey(record));
    }

    
    public boolean updateByPrimaryKeySelective(T record) {
        return retBool(this.abstractMapper.updateByPrimaryKeySelective(record));
    }

    
    public boolean updateByExample(T record, Example example) {
        return retBool(this.abstractMapper.updateByExample(record, example));
    }

    public boolean updateByExampleSelective(T record, Example example) {
        return retBool(this.abstractMapper.updateByExampleSelective(record, example));
    }

    public T selectByPrimaryKey(Serializable key) {
        return this.abstractMapper.selectByPrimaryKey(key);
    }

    public T selectOne(T record) {
        return this.abstractMapper.selectOne(record);
    }

    public List<T> selectAll() {
        return this.abstractMapper.selectAll();
    }

    public PageInfo<T> selectPage(int page, int rows, Example example) {
        if (page < 0) {
            page = 0;
        }
        if (rows <= 0 || rows > 100) {
            rows = 100;
        }
        PageHelper.startPage(page, rows);
        List<T> list = this.abstractMapper.selectByExample(example);
        return new PageInfo(list);
    }

    public int selectCount(T record) {
        return this.abstractMapper.selectCount(record);
    }

    public List<T> select(T record) {
        return this.abstractMapper.select(record);
    }

    public int selectCountByExample(Example example) {
        return this.abstractMapper.selectCountByExample(example);
    }

    public List<T> selectByExample(Example example) {
        return this.abstractMapper.selectByExample(example);
    }

    public List<T> selectByExampleAndRowBounds(Example example, RowBounds rowBounds) {
        return this.abstractMapper.selectByExampleAndRowBounds(example, rowBounds);
    }

    public List<T> selectByRowBounds(T record, RowBounds rowBounds) {
        return this.abstractMapper.selectByRowBounds(record, rowBounds);
    }

}
